package com.glory.dgcpcpda.activity.cpc;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.activity.recipe.TERecipeDBWBActivity;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.callback.CustomDialogEntityCallBack;
import com.glory.dgcpcpda.entity.EqpInfoEntity;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

public class WBrejectsActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    @BindView(R.id.etlOTID)
    FreeUI_ClearEditText etlOTID;
    @BindView(R.id.etEqpWorkID)
    FreeUI_ClearEditText etEqpWorkID;
    @BindView(R.id.etkz)
    FreeUI_ClearEditText etkz;
    @BindView(R.id.etpj)
    FreeUI_ClearEditText etpj;
    @BindView(R.id.etdbz)
    FreeUI_ClearEditText etdbz;
    @BindView(R.id.etdedtl)
    FreeUI_ClearEditText etdedtl;
    @BindView(R.id.etzw)
    FreeUI_ClearEditText etzw;
    @BindView(R.id.etts)
    FreeUI_ClearEditText etts;
    @BindView(R.id.etqt)
    FreeUI_ClearEditText etqt;
    @BindView(R.id.etkjbx)
    FreeUI_ClearEditText etkjbx;
    @BindView(R.id.etbyxp)
    FreeUI_ClearEditText etbyxp;
    @BindView(R.id.etxppy)
    FreeUI_ClearEditText etxppy;
    @BindView(R.id.etlp)
    FreeUI_ClearEditText etlp;

    private LotInfoEntity mLotInfoEntity;
    private EqpInfoEntity eqpInfoEntity;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_cpc_wbblp;
    }

    @Override
    protected void initView() {
        tvTitle.setText("键合不良品上报");
        setMainLayout(llMain);

        tvBaseSubmit.setText("提交");

        initEdit(etlOTID, s -> LotUtils.searchLotInfo(WBrejectsActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
            @Override
            public void onSuccess(LotInfoEntity response) {
                mLotInfoEntity = response;
                etEqpWorkID.requestFocus();
            }

            @Override
            public void onFail(Throwable t) {
                loadError(t, "LOTQUERY");
            }
        }));

        initEdit(etEqpWorkID, s -> LotUtils.searchEqpInfo(s,WBrejectsActivity.this,  getSupportFragmentManager(), new LotUtils.netCallback<EqpInfoEntity>() {
            @Override
            public void onSuccess(EqpInfoEntity response) {
                eqpInfoEntity = response;
                etkz.requestFocus();
            }

            @Override
            public void onFail(Throwable t) {
                loadError(t, "EQPQUARY");
            }
        }));

        eqpInfoEntity = new EqpInfoEntity();
        mLotInfoEntity = new LotInfoEntity();

        etlOTID.requestFocus();
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.tvBaseSubmit:
                if (mLotInfoEntity.getLOT().getLOTID().equals("")){
                    showToast("批次号不能为空",false);
                    return;
                }

                if (eqpInfoEntity.getEQUIPMENT().getEQUIPMENTID().equals("")){
                    showToast("设备号不能为空",false);
                    return;
                }

                FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                        (new TypeToken<BaseResponseBean<JSONObject>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                            showToast("上报成功",true);
                            finish();
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<JSONObject>> response) {
                        super.onError(response);
                        loadError(response.getException(),"WBrejects");
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("LOTID",mLotInfoEntity.getLOT().getLOTID());
                map.addProperty("KHDM",mLotInfoEntity.getLOT().getCUSTOMERCODE());
                map.addProperty("FZXS",mLotInfoEntity.getPACKAGETYPE());
                map.addProperty("DLM",mLotInfoEntity.getLOT().getUDF().getRESERVED2());
                map.addProperty("MAINQTY",mLotInfoEntity.getLOT().getMAINQTY());
                map.addProperty("EQUIPMENTID",eqpInfoEntity.getEQUIPMENT().getEQUIPMENTID());
                map.addProperty("JTH",eqpInfoEntity.getEQUIPMENT().getLOCATION());
                map.addProperty("KZ",getText(etkz));
                map.addProperty("PJ",getText(etpj));
                map.addProperty("DBZ",getText(etdbz));
                map.addProperty("DEDTL",getText(etdedtl));
                map.addProperty("ZW",getText(etzw));
                map.addProperty("TS",getText(etts));
                map.addProperty("QT",getText(etqt));
                map.addProperty("KJBX",getText(etkjbx));
                map.addProperty("BYXP",getText(etbyxp));
                map.addProperty("XPPY",getText(etxppy));
                map.addProperty("LP",getText(etlp));
                NetUtils.requestPostCPC("CPC.WBrejects","WBrejects",this,map,callBack);
                break;
        }

    }

}
